<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 16:01
 */

namespace app\admin\server;


use think\Cache;
use think\Controller;

class AccessToken extends Controller
{
    protected static $appid='wxf409b1cfe18a1419';//微信的appid
    protected static $secret='73effea32965305e1f8970db68d87c9a';//微信的开发者密钥
    //获取accesstoken
    public static function  getAccessToken()
    {
        if (self::ishastoken()){
            return Cache::store('redis')->get(self::$appid);
        }else{
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::$appid."&secret=".self::$secret;
            $result = http_curl($url);
            $value = $result['access_token'];
            Cache::store('redis')->set(self::$appid,$value,7000);
            return Cache::store('redis')->get(self::$appid);
        }
    }

    //判断是否存在和过期
    public static function ishastoken()
    {
        //从缓存里获取accesstoken 判断是否已过期或者是否存在
        $ishas=Cache::store('redis')->get(self::$appid);
        if ($ishas){
            return true;
        }else{
            return false;
        }
    }


}